/*
 ------------------------------------------------------------------------------
 Copyright (C) 2013 Eternal Games.

 This file is part of the EternalTech source code.

 The EternalTech source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The EternalTech source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the EternalTech source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


//
// cl_screen.cpp - screen elements drawing
//


#include "client.h"


/*
 ==================
 CL_DrawCinematic
 ==================
*/
static bool CL_DrawCinematic (){

	cinData_t	data;

	// Check if playing a cinematic
	if (!cls.playingCinematic)
		return false;

	// Run a frame
	data = CIN_UpdateCinematic(cls.cinematicHandle, cls.frameTime);

	while (data.image == NULL){
		// Stop it and play the next cinematic, if any
		CL_NextCinematic();

		// Check if playing a cinematic
		if (!cls.playingCinematic)
			return false;

		// Run a frame
		data = CIN_UpdateCinematic(cls.cinematicHandle, cls.frameTime);
	}

	// Draw it
	R_DrawCinematic(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, H_SCALE, 1.0f, V_SCALE, 1.0f, cls.cinematicHandle, data.image, data.dirty, data.width, data.height, true);

	return true;
}

/*
 ==================
 
 ==================
*/
static void CL_DrawMainMenu (){

}

/*
 ==================
 CL_DrawFPS
 ==================
*/
static void CL_DrawFPS (){

	static int	previousTime;
	int			time, totalTime;
	int			fps;
	char		string[16];
	glqVec4		color;
	int			len;
	int			i;

	time = Sys_Milliseconds();

	cls.fpsTimes[cls.fpsIndex & (FPS_FRAMES - 1)] = time - previousTime;
	cls.fpsIndex++;

	previousTime = time;

	if (!cl_drawFPS->integerValue || cls.fpsIndex <= FPS_FRAMES)
		return;

	// Average multiple frames together to smooth changes out a bit
	totalTime = 0;

	for (i = 0; i < FPS_FRAMES; i++)
		totalTime += cls.fpsTimes[i];

	if (totalTime < 1)
		totalTime = 1;

	fps = 1000 * FPS_FRAMES / totalTime;
	if (fps > 1000)
		fps = 1000;

	// Set the string
	len = Str_SPrintf(string, sizeof(string), "%i FPS", fps);

	// Set the color
	if (fps >= 60)
		color.Set(1.0f);
	else {
		if (fps < 30)
			color.Set(1.0f, (float)fps / 30.0f, 0.0f, 1.0f);
		else
			color.Set(1.0f, 1.0f, (float)(fps - 30) / 30.0f, 1.0f);
	}

	// Draw it
	R_DrawString((SCREEN_WIDTH - len * 12.0f) - 6.0f, 6.0f, 12.0f, 12.0f, string, color, true, 2.0f, 2.0f, H_ALIGN_RIGHT, 1.0f, V_ALIGN_TOP, 1.0f, cls.charsetMaterial);
}


// ============================================================================


/*
 ==================
 
 This is called every frame, but can also be called explicitly to flush
 graphics to the screen
 ==================
*/
void CL_UpdateScreen (){

	// Begin a new frame
	R_BeginFrame(cls.frameTime);

	switch (cls.state){
	case CS_DISCONNECTED:
		// If playing a cinematic, draw it
		if (CL_DrawCinematic())
			break;

		// Fall through

	case CS_CONNECTING:
	case CS_CHALLENGING:
	case CS_CONNECTED:
		// Draw the main menu GUI
		CL_DrawMainMenu();

		break;
	case CS_LOADING:
	case CS_PRIMED:
		// Let the game module draw whatever it wants during loading
		CG_Redraw();

		break;
	case CS_ACTIVE:
		// Draw the game view
		CG_Redraw();

		break;
	}

	// Draw the FPS display
	CL_DrawFPS();

	// The console will be drawn on top of everything
	Con_Redraw();

	// End the frame
	R_EndFrame();
}